import * as Cesium from 'cesium';

class BDRegionCode {
  constructor(viewer) {
    this.defaultPos = [114.9358888888, 25.8458888888, 1000]
    this.viewer = viewer;
    this.currentPos = this.defaultPos
    this.codeCfg = [
      {
        code: [['S', 'N']],
        cell: [180, 90],
        highPrecision: [1327104000, 663552000]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
        ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V']],
        cell: [6, 4],
        highPrecision: [44236800, 29491200]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 2, 1 / 2],
        highPrecision: [3686400, 3686400]
      },
      {
        code: [['0', '1', '2', '3', '4', '5']],
        cell: [1 / 4, 1 / 6],
        highPrecision: [1843200, 1228800]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']],
        cell: [1 / 60, 1 / 60],
        highPrecision: [122880, 122880]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E'],
        ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E']],
        cell: [1 / 900, 1 / 900],
        highPrecision: [8192, 8192]
      },
      {
        code: [['0', '1', '2', '3']],
        cell: [1 / 1800, 1 / 1800],
        highPrecision: [4096, 4096]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 14400, 1 / 14400],
        highPrecision: [512, 512]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 115200, 1 / 115200],
        highPrecision: [64, 64]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 921600, 1 / 921600],
        highPrecision: [8, 8]
      },
      {
        code: [['0', '1', '2', '3', '4', '5', '6', '7'], ['0', '1', '2', '3', '4', '5', '6', '7']],
        cell: [1 / 7372800, 1 / 7372800],
        highPrecision: [1, 1]
      },
    ]
  }
  flyToDefaultPos () {
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(...this.defaultPos),
      orientation: {
        heading: Cesium.Math.toRadians(0.0),
        pitch: Cesium.Math.toRadians(-90.0),
        roll: 0.0
      }
    });
  }
  getGridCodeBD (posArr = this.defaultPos) {
    this.currentPos = posArr
    this.codeCfg.forEach((element, index) => {
      this.getLevelCode(index, element)
    });
  }
  getLevelCode (index, element) {
    console.log(`getLevelCode->${index}`, element);
    if (index === 0) {
      console.log(this[`getCodeLevel${index}`](this.defaultPos));
    }
    if (index === 1) {
      // console.log(this[`getCodeLevel${index}`](this.defaultPos));
    }
  }
  getCodeLevel0 (posArr) {
    const latD = posArr[1] > 0 ? 'N' : posArr[1] < 0 ? 'S' : '0'
    // 向下取整(精度待优化)(定义一个三位数组[前、中、后])
    const c1 = Math.round(Math.abs(posArr[0]) / 6) + 1
    const c2 = Math.round(Math.abs(posArr[1]) / 4) + 1
    const str2 = String.fromCharCode(c2 + 64)
    return `${latD}${c1}${str2}`
  }
  getCodeLevel1 (posArr, latDirection) {

  }
}

export default BDRegionCode;
